﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using HtmlAgilityPack;
using LenderClearingHouse;

namespace LCHWinScraper.SiteScrapers
{
	public class RECON: SiteScraper
	{
		public override string Name { get { return "Recon Trust"; } }
		public override string Value { get { return "RECON"; } }
		public override string StartUrl { get { return "https://www.recontrustco.com/upcoming_counties.aspx?state=Nevada"/*"http://www.recontrustco.com/index.aspx"*/; } }
		public override string DataUrl { get { return "https://www.recontrustco.com/upcoming_counties.aspx?state=Nevada"; } }
		public override string HtmlContent { get; set; }

		public override void ParseDataProcess()
		{
			string html = GetHtmlFromUrl(DataUrl);

			var req = (HttpWebRequest)WebRequest.Create(DataUrl);
			req.CookieContainer = new CookieContainer();
			string pageHtml = "";
			using (var resp = (HttpWebResponse)req.GetResponse())
			{
				using (var sr = new StreamReader(resp.GetResponseStream()))
				{
					pageHtml = sr.ReadToEnd();
				}
			}

			html = pageHtml.Replace("\r\n", "").Replace("\t", "");

			var doc = new HtmlDocument();
			doc.LoadHtml(html);

			
			foreach (HtmlNode cell in doc.DocumentNode.SelectNodes("//table[@id='ctl00_ContentPlaceHolder1_dgCountyProperty']//a[@class='propBoxLinks_li_a']"))
			{
				string link = string.Format("http://www.recontrustco.com/{0}", cell.Attributes["href"].Value.Trim());

				var req2 = (HttpWebRequest)WebRequest.Create(link);
				req2.CookieContainer = req.CookieContainer;

				using (var resp = (HttpWebResponse)req2.GetResponse())
				{
					using (var sr = new StreamReader(resp.GetResponseStream()))
					{
						pageHtml = sr.ReadToEnd();
					}
				}

				var table = PageScrapes.scrapeReconTrust(/*GetHtmlFromUrl(link)*/pageHtml);
				ImportDataAccess.BulkCopyOpeningBid(table);
			}
		}

		private string GetHtmlFromUrl(string url)
		{
			var req = (HttpWebRequest)WebRequest.Create(url);

			string pageHtml = "";
			using (var resp = (HttpWebResponse)req.GetResponse())
			{
				using (var sr = new StreamReader(resp.GetResponseStream()))
				{
					pageHtml = sr.ReadToEnd();
				}
			}

			return pageHtml.Replace("\r\n", "").Replace("\t", "");
		}
	}
}
